package rs.mbs2.epics.server.servlet.util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageUtil {
	
	private static final int RESIZE_WIDTH = 150;
	private static final int RESIZE_HEIGHT = 150;
	
	public static void deleteFolderFromDisk(String path) {
		
	}
	
	public static void deleteFileFromDisk(File file) {
		boolean result = file.delete();
		System.out.println("ImageUtil.deleteFileFromDisk()");
		System.out.println(result);
	}
	
	public static String createImagePaths(String userFolderPath, String folderName, String fileName) {
		
		File userFolderTest = new File(userFolderPath);
		
		if(!userFolderTest.exists()) {	// ako ne postoji folder, treba da se napravi
			if(userFolderTest.mkdir()) {
				System.out.println("Napravljeno: " + userFolderPath);
				
				//uploadedFile = new File(filePath);
			} else {
				System.out.println("Greska, folder nije napravljena na: " + userFolderPath);
			}
		} else {	// ako već postoji folder
			
		}
		
		String folderForImagePath = userFolderPath + "/" + folderName;
		File folderForImageTest = new File(folderForImagePath);
		
		if(!folderForImageTest.exists()) {	// ako ne postoji folder, treba da se napravi
			if(folderForImageTest.mkdir()) {
				System.out.println("Napravljeno: " + folderForImagePath);
				
				//uploadedFile = new File(filePath);
			} else {
				System.out.println("Greska, folder nije napravljena na: " + folderForImagePath);
			}
		} else {	// ako već postoji folder
			
		}
		

		
		String filePath = folderForImagePath + "/" + fileName;
		
		return filePath;
	}
	
	public static String getThumbnailPath(String userFolderPath, String folderName, String fileName) {
		String thumbnailFolderPath = userFolderPath + "/" + folderName + "/thumbnails";
		File thumbnailFolderTest = new File(thumbnailFolderPath);
		
		if(!thumbnailFolderTest.exists()) {
			if(thumbnailFolderTest.mkdir()) {
				System.out.println("Napravljeno: " + thumbnailFolderPath);
			} else {
				System.out.println("Greska, folder nije napravljena na: " + thumbnailFolderPath);
			}
		}
		
		String thumbnailFilePath = thumbnailFolderPath + "/" + fileName;
		
		return thumbnailFilePath;
	}
	
	public static BufferedImage getThumbnail(String filePath, BufferedImage image) throws IOException {
		
		System.out.println("Thumbnail path " + filePath);
		
		int w = image.getWidth();
		int h = image.getHeight();
		
		double proportion = (double)h / w;
        int height;
        int width;
        if (proportion < 1) {
            width = RESIZE_WIDTH;
            height = (int)(width * proportion);
        } else {
            height = RESIZE_HEIGHT;
            width = (int)(height / proportion);
        }
        
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Image scaledImage = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
		img.createGraphics().drawImage(scaledImage, 0, 0, null);
		BufferedImage img2 = new BufferedImage(width, height ,BufferedImage.TYPE_INT_RGB);
		img2 = img.getSubimage(0, 0, width, height);
		
		return img2;
	}
}
